package com.zrwl.poscloud.core.config.admin;

import com.zrwl.poscloud.xo.service.MenusService;
import com.zrwl.poscloud.xo.vo.MenusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 本地服务器完成的Api路径配置
 *
 * @author liusitong
 * @date 2022年3月22日
 */
@Configuration
public class LocalApiConfig {

    @Autowired
    private MenusService menusService;

    @Value(value = "${server.local-mode}")
    boolean localMode;
    static List<String> cloudApiList = new ArrayList<>();
    static List<String> localApiList = new ArrayList<>();

    public boolean isLocalApi(String url) {
        initializeLocalAndCloudUrl();
        if (localMode) {
            return false;
        }
        boolean localRequestAll = false;
        boolean cloudRequestAll = false;
        boolean localRequest = false;
        boolean cloudRequest = false;
        String[] requestMap = url.substring(1).split("/");
        if (localApiList.contains("/" + requestMap[0] + "/*")) {
            localRequestAll = true;
        }
        if (localApiList.contains(url)) {
            localRequest = true;
        }
        if (cloudApiList.contains("/" + requestMap[0] + "/*")) {
            cloudRequestAll = true;
        }
        if (cloudApiList.contains(url)) {
            cloudRequest = true;
        }
        /***
         * 遵循本地优先级最高规则
         */
        if (localRequest) {
            return true;//本地有指定，用本地
        } else if (localRequestAll && cloudRequest) {
            return false;//本地有全部，云端有指定，用云端
        } else if (localRequestAll && cloudRequestAll) {
            return true;//本地有全部，云端有全部，用本地
        } else if (localRequestAll) {
            return true;//本地有全部，云端无，用本地
        }
        return false;//本地没有，云端也没有，或者本地没有，云端有，都用云端即可
    }

    public void changeLocalAndCloudUrl(MenusVO menusVO) {
        if (!localMode) {
            if (menusVO.getMenuType() == 1) {
                if (cloudApiList.size() == 0 || localApiList.size() == 0) {
                    Map<Integer, List<String>> map = menusService.getLocalOrCloudUrlMode();
                    if (map == null || map.size() == 0) {
                        throw new RuntimeException("无权限");
                    } else {
                        cloudApiList = map.get(0);
                        localApiList = map.get(1);
                    }
                } else {
                    if (menusVO.getLocalMode() == 0) {
                        if (!cloudApiList.contains(menusVO.getUrl())) {
                            cloudApiList.add(menusVO.getUrl());
                        }
                        localApiList.removeIf(url -> url.equals(menusVO.getUrl()));
                    } else {
                        if (!localApiList.contains(menusVO.getUrl())) {
                            localApiList.add(menusVO.getUrl());
                        }
                        cloudApiList.removeIf(url -> url.equals(menusVO.getUrl()));
                    }
                }
            }
        }
    }

    public void initializeLocalAndCloudUrl() {
        if (!localMode) {
            if (cloudApiList.size() == 0 || localApiList.size() == 0) {
                Map<Integer, List<String>> map = menusService.getLocalOrCloudUrlMode();
                if (map == null || map.size() == 0) {
                    throw new RuntimeException("无权限");
                } else {
                    cloudApiList = map.get(0);
                    localApiList = map.get(1);
                }
            }
        }
    }
}
